using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using NUnit.Framework;

namespace DatabaseUnitTesting.Utilities.Tests
{
    [TestFixture]
    public class DatabaseAdapterTests
    {
        private string database;
        private string snapshot;
        private string server;
        private SqlConnection connection;
        private DatabaseAdapter databaseAdapter;

        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            database = "astyler";
            server = "(local)";
            snapshot = "tempSS";
            string connectionString = "Application Name=UnitTests;Trusted_Connection=true;Server=" + server +
                                      ";Database=" + database;
            connection = new SqlConnection(connectionString);
            connection.Open();
            databaseAdapter = new DatabaseAdapter(connection);
        }

        [Test]
        public void ConnectionValidationTest()
        {
            connection.Close();

            databaseAdapter.ValidateConnection();

            Assert.AreEqual(ConnectionState.Open, connection.State);
        }

        [Test]
        public void TestExistence()
        {
            databaseAdapter.CreateSnapshot(database, snapshot);

            Assert.AreEqual(Existence.Database, databaseAdapter.TestExistence(database));
            Assert.AreEqual(Existence.Snapshot, databaseAdapter.TestExistence(snapshot));
            Assert.AreEqual(Existence.No, databaseAdapter.TestExistence("FakeObject"));

            databaseAdapter.DropSnapshot(snapshot);
        }

        [Test]
        public void UseDatabaseTest()
        {
            databaseAdapter.UseDatabase(database);
            Assert.AreEqual(database, connection.Database);
            databaseAdapter.UseDatabase("astyler1");
            Assert.AreEqual("astyler1", connection.Database);

         
            databaseAdapter.UseDatabase(database);
        }

        [Test]
        public void CreateSnapshotLowLevelTest()
        {
            Assert.AreEqual(Existence.Database, databaseAdapter.TestExistence(database));
            Assert.AreEqual(Existence.No, databaseAdapter.TestExistence(snapshot));

            databaseAdapter.CreateSnapshot(database, snapshot);
            Assert.AreEqual(Existence.Database, databaseAdapter.TestExistence(database));
            Assert.AreEqual(Existence.Snapshot, databaseAdapter.TestExistence(snapshot));

            databaseAdapter.DropSnapshot(snapshot);
        }

        [Test]
        public void DropSnapshotLowLevelTest()
        {
            Assert.AreEqual(Existence.No, databaseAdapter.TestExistence(snapshot));

            databaseAdapter.CreateSnapshot(database, snapshot);
            Assert.AreEqual(Existence.Snapshot, databaseAdapter.TestExistence(snapshot));

            databaseAdapter.DropSnapshot(snapshot);
            Assert.AreEqual(Existence.No, databaseAdapter.TestExistence(snapshot));
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            connection.Close();
        }
    }
}